<?php
App::import('Vendor','xtcpdf'); 

$tcpdf = new XTCPDF();
$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans'


// set document information
$tcpdf->SetCreator(PDF_CREATOR);
$author = $vorname." ".$name;
$tcpdf->SetAuthor($author);
$projectname = $pname." - Mass Swap";
$tcpdf->SetTitle($projectname);
$tcpdf->SetSubject('Detail Report Interactions');

// set page properties
$tcpdf->SetAutoPageBreak(false);
$tcpdf->SetPageOrientation('L');
$tcpdf->setPrintHeader(true);
$tcpdf->setPrintFooter(false);
$tcpdf->SetFont('helvetica');

// set header details
$projectname = $pname." - Mass Swap";
$tcpdf->xheadertext = $projectname;
$tcpdf->xheadercolor = array(110, 183, 228);

// add a page
$tcpdf->AddPage();

// page content
$tcpdf->SetTextColor(146, 139, 129);
$tcpdf->SetFontSize(28);
$tcpdf->Text(30,36,'Detail Report Interactions');

$maundabt = $vorname." ".$name.", Order Monitoring";
$datumandort = date("d.m.Y").", Otelfingen";
$tcpdf->SetFontSize(16);
$tcpdf->Text(30,60,$maundabt);
$tcpdf->Text(30,67,$datumandort);
$tcpdf->Text(30,90,"Project description: ".$pdescription);
$tcpdf->Image(IMAGES . 'upc-cablecom.jpg', 250, 165, 45, 45, 'JPG');

// page end
$tcpdf->endPage();

// set page properties
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(true);

// add a page
$tcpdf->AddPage();

// set table properties
$tcpdf->setCellPaddings(1, 1, 1, 1);
$tcpdf->SetFillColor(110, 183, 228);
$tcpdf->SetFontSize(11);

$titel1 = "Run ID";
$titel2 = "Startdate";
$titel3 = "Total Swaps";
$titel4 = "Skipped Swaps";
$titel5 = "Ongoing Swaps";
$titel6 = "Completed Swaps";
$titel7 = "Interactions";
$titel8 = "Batch Status";

$tcpdf->SetTextColor(255, 255, 255,'',false);
$tcpdf->SetFillColor(168, 212, 239);
$tcpdf->SetFont('helvetica', 'B');

$tcpdf->MultiCell(30,  12, $titel1          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel2          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel3          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel4          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel5          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel6          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel7          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  12, $titel8          , 1, 'L', 1, 1, '', '', true);
$tcpdf->SetTextColor(0, 0, 0,'',false);
$tcpdf->SetFont('helvetica');

$i = 0; $total1 = 0; $total2 = 0; $total3 = 0; $total4 = 0; $total5 = 0;
WHILE($row = mysql_fetch_array($result))
  {
	if($i == 20) {
		$tcpdf->SetTextColor(146, 139, 129,'',false);
		$tcpdf->SetFontSize(14);
		$snapshot = "Data snapshot: ".date("d.m.Y H:i");
		$tcpdf->Text(10,176,$snapshot);
		$tcpdf->Text(10,183,'A legend about the figures can be found on the last page');
		// page end
		$tcpdf->endPage();
		// set page properties
		$tcpdf->setPrintHeader(false);
		$tcpdf->setPrintFooter(true);
		// add a page
		$tcpdf->AddPage();
		// set table properties
		$tcpdf->setCellPaddings(1, 1, 1, 1);
		$tcpdf->SetFillColor(110, 183, 228);
		$tcpdf->SetFontSize(11);

		$titel1 = "Run ID";
		$titel2 = "Startdate";
		$titel3 = "Total Swaps";
		$titel4 = "Skipped Swaps";
		$titel5 = "Ongoing Swaps";
		$titel6 = "Completed Swaps";
		$titel7 = "Interactions";
		$titel8 = "Batch Status";

		$tcpdf->SetTextColor(255, 255, 255,'',false);
		$tcpdf->SetFillColor(168, 212, 239);
		$tcpdf->SetFont('helvetica', 'B');
		$tcpdf->MultiCell(30,  12, $titel1          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel2          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel3          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel4          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel5          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel6          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel7          , 1, 'L', 1, 0, '', '', true);
		$tcpdf->MultiCell(30,  12, $titel8          , 1, 'L', 1, 1, '', '', true);
		$tcpdf->SetTextColor(0, 0, 0,'',false);
		$i = 0;
		$tcpdf->SetFont('helvetica');
	}
	
	if ($i++ % 2 == 0) { $tcpdf->SetFillColor(232, 231, 237); } else { $tcpdf->SetFillColor(226, 241, 250); }

	$rid = $row["id"];
	$batchname = "Batch ".$rid;
	$startdate = $batch->startdatum($rid);
	$total = $batch->total($rid);
	$total1 = $total1 + $batch->total($rid);
	$skipped = $batch->skipped($rid);
	$total2 = $total2 + $batch->skipped($rid);
	IF($skipped == 0) { $skipped2 = 0; } ELSE { $skipped2 = round(100/$total*$skipped,0); }
	$skipped = $skipped." (".$skipped2."%)";
	$open = $batch->open($rid);
	$total3 = $total3 + $batch->open($rid);
	IF($open == 0) { $open2 = 0; } ELSE { $open2 = round(100/$total*$open,0); }
	$open = $open." (".$open2."%)";
	$completed = $batch->completed($rid);
	$total4 = $total4 + $batch->completed($rid);
	IF($completed == 0) { $completed2 = 0; } ELSE { $completed2 = round(100/$total*$completed,0); }
	$completed = $completed." (".$completed2."%)";
	$interactions = $batch->interactionstotal($rid);
	$total5 = $total5 + $batch->interactionstotal($rid); 
	$status = $batch->batchstatus($rid);
	
	
	
	$tcpdf->MultiCell(30,  5, $batchname      , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $startdate      , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $total          , 1, 'L', 1, 0, '', '', true);;
	$tcpdf->MultiCell(30,  5, $skipped        , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $open           , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $completed      , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $interactions   , 1, 'L', 1, 0, '', '', true);
	$tcpdf->MultiCell(30,  5, $status         , 1, 'L', 1, 1, '', '', true);
  }
if ($i++ % 2 == 0) { $tcpdf->SetFillColor(232, 231, 237); } else { $tcpdf->SetFillColor(226, 241, 250); }
$prozskipped = round((100/$total1*$total2),0); $pskipped = $total2." (".$prozskipped."%)";
$prozongoing = round((100/$total1*$total3),0); $pongoing = $total3." (".$prozongoing."%)";
$pozcompleted = round((100/$total1*$total4),0); $pcompleted = $total4." (".$pozcompleted."%)";
$tcpdf->SetFont('helvetica', 'B');
$tcpdf->MultiCell(30,  5, 'TOTAL'     , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, ''          , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, $total1     , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, $pskipped   , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, $pongoing   , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, $pcompleted , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, $total5     , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(30,  5, ''          , 1, 'L', 1, 1, '', '', true);
$tcpdf->SetFont('helvetica');

$tcpdf->SetTextColor(146, 139, 129,'',false);
$tcpdf->SetFontSize(14);
$snapshot = "Data snapshot: ".date("d.m.Y H:i");
$tcpdf->Text(10,176,$snapshot);
$tcpdf->Text(10,183,'A legend about the figures can be found on the last page');


// page end
$tcpdf->endPage();
// set page properties
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(true);
// add a page
$tcpdf->AddPage();

$tcpdf->SetFont('helvetica', 'B');
$tcpdf->SetFontSize(14);
$tcpdf->setCellPaddings(1, 1, 1, 1);
$tcpdf->SetTopMargin(30);
$tcpdf->SetLeftMargin(30);
$tcpdf->MultiCell(50,  5, 'Legend'   , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, ''   , 0, 'L', 0, 1, '', '', true);
$tcpdf->SetFont('helvetica');
$tcpdf->SetFontSize(11);

$text1 = "Total MACs in the batch.";
$text2 = "CPE is already swapped or cancelled in the meantime.";
$text3 = "Swap order is still ongoing in Clariy.";
$text4 = "CPE is swapped and the order is successfully completed.";
$text5 = "Interactions created per batch.";

$tcpdf->MultiCell(50,  5, 'Total Swaps'     , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, $text1            , 0, 'L', 0, 1, '', '', true);
$tcpdf->MultiCell(50,  5, 'Skipped Swaps'   , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, $text2            , 0, 'L', 0, 1, '', '', true);
$tcpdf->MultiCell(50,  5, 'Ongoing Swaps'   , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, $text3            , 0, 'L', 0, 1, '', '', true);
$tcpdf->MultiCell(50,  5, 'Completed Swaps' , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, $text4            , 0, 'L', 0, 1, '', '', true);
$tcpdf->MultiCell(50,  5, 'Interactions'    , 0, 'L', 0, 0, '', '', true);
$tcpdf->MultiCell(200, 5, $text5            , 0, 'L', 0, 1, '', '', true);


$projectname2 = $pname."-Detail-Report.pdf";
echo $tcpdf->Output($projectname2, 'D');
?>